<template>
  <div class="sign">
    <tips :tips="tips"></tips>
    <div class="sign-table">
      <el-select
        v-model="status_audit"
        placeholder="请选择"
        @change="selectStatus"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-date-picker
        v-model="dateRange"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
        value-format="timestamp"
        @change="selectDate"
      ></el-date-picker>

      <el-button type="primary" @click="buySign" class="buy-sign-btn">
        购买签名
      </el-button>
    </div>

    <el-table
      :data="tableData"
      :header-cell-style="headerCellStyle"
      style="width: 100%"
    >
      <el-table-column prop="sign" label="签名"></el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag type="info" effect="dark" v-if="scope.row.status == '2'">
            {{ scope.row.status_test }}
          </el-tag>
          <el-tag
            type="success"
            effect="dark"
            v-else-if="scope.row.status == '3'"
          >
            {{ scope.row.status_test }}
          </el-tag>
          <el-tooltip
            class="item"
            effect="light"
            :content="'拒绝原因：' + scope.row.refuse_reason"
            placement="bottom"
            v-else-if="scope.row.status == '-1'"
          >
            <el-tag type="danger" effect="dark">
              {{ scope.row.status_test }}
            </el-tag>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="时间"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            @click="reExamination(scope.row)"
            v-if="scope.row.status == -1"
          >
            重新审核
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="deleteSign(scope.row)"
            v-if="scope.row.can_remove"
          >
            删除签名
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <!-- 签名重新审核，新签名弹窗 -->
  </div>
</template>

<script>
import tips from "./tips.vue";
import { getSignList, reAuditSign, signRemove } from "@/api/sign";
export default {
  name: "Sign",
  components: {
    tips
  },
  props: {
    site_key: {
      type: Number,
      default() {
        return null;
      }
    },
    js_ticket: {
      type: String,
      default() {
        return null;
      }
    }
  },
  data() {
    return {
      status_audit: "",
      options: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "2",
          label: "审核中"
        },
        {
          value: "3",
          label: "审核通过"
        },
        {
          value: "-1",
          label: "拒绝"
        }
      ],
      tableData: [],
      dateRange: [new Date(), new Date()],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      headerCellStyle: {
        background: "#f7f8fa",
        height: "40px"
      },
      tips: [
        "发送短信内容时必须带签名，未创建签名。系统自动默认签名为【微擎】",
        "创建签名前请去应用市场购买签名，并提交审核。审核未通过，自动退款至用户交易币",
        "提交签名审核，预计2小时完成审核。审核时间为周一至周六9:00-18:00（不包含法定节假日）"
      ],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      sms_sign: ""
    };
  },
  methods: {
    buySign() {
      window.open("https://s.w7.cc/goods-7.html?type=1", "_blank");
    },
    selectDate() {
      this.getSignList();
    },
    selectStatus() {
      this.getSignList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getSignList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getSignList();
    },
    getSignList() {
      let params = {
        js_ticket: this.js_ticket,
        site_key: this.site_key,
        start_time:
          this.dateRange && this.dateRange.length
            ? parseInt(new Date(this.dateRange[0]).getTime() / 1000)
            : "",
        end_time:
          this.dateRange && this.dateRange.length
            ? parseInt(new Date(this.dateRange[1]).getTime() / 1000)
            : "",
        status_audit: this.status_audit,
        page_size: this.pageSize,
        page: this.currentPage
      };
      getSignList(params).then(res => {
        this.tableData = res.data;
        this.total = res.total;
      });
    },
    reExamination(row) {
      let params = {
        js_ticket: this.js_ticket,
        site_key: this.site_key,
        sign_id: row.id,
        sms_sign: ""
      };
      this.$prompt("请输入新短信签名", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /\S/,
        inputErrorMessage: "短信签名不能为空"
      })
        .then(({ value }) => {
          params.sms_sign = value;
          reAuditSign(params)
            .then(res => {
              console.log(res);
              if (res.errno == 0) {
                this.$message({
                  type: "error",
                  message: res.message
                });
              } else {
                this.$message({
                  type: "success",
                  message: res.message
                });
              }
            })
            .finally(() => {
              this.getSignList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    },
    deleteSign(row) {
      let params = {
        js_ticket: this.js_ticket,
        site_key: this.site_key,
        sign_id: row.id
      };
      this.$confirm("此操作将永久删除该签名, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          signRemove(params)
            .then(res => {
              if (res.errno == 0) {
                this.$message({
                  type: "error",
                  message: "删除失败！"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "删除成功！"
                });
              }
            })
            .finally(() => {
              this.getSignList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-select {
  margin-right: 30px;
}

.buy-sign-btn {
  float: right;
}
.sign-table {
  margin-bottom: 30px;
}
</style>
